package cn.tp.ksoft.civil_driver;

import android.app.Service;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.request.TokenBroadcasrReceiver;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.library_civil.view.BaseActivity;

/**
 * Created by zhusong on 16/5/1.
 */
public class DriverBaseActivity extends BaseActivity implements RequestModules {

    protected TokenBroadcasrReceiver receiver = new TokenBroadcasrReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.FILTER_SUFFIX);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
    }

    protected void updateTitle(String title) {
        TextView mTv = ((TextView)findViewById(R.id.tv_title));
        if (null != mTv) {
            mTv.setText(title);
        }
        View backBtn = findViewById(R.id.btn_back);
        if (null != backBtn) {
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }

    }
    protected void hideSoftInput(){
        InputMethodManager imm = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
        View focusView = getCurrentFocus();
        if (null != focusView) {
            imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        hideSoftInput();
        return super.onTouchEvent(event);
    }

    protected String getStatus(int status) {
        if (status == Constants.ORDER_STATUS_WAIT) {
            return getString(R.string.driver_order_status_wait);
        } else if (status == Constants.ORDER_STATUS_RECEIPT) {
            return getString(R.string.driver_order_status_receipt);
        } else if (status == Constants.ORDER_STATUS_FINISH) {
            return getString(R.string.driver_order_status_finish);
        } else if (status == Constants.ORDER_STATUS_CLOSE) {
            return getString(R.string.driver_order_status_close);
        }
        return null;
    }

    protected static class WeakHandler extends Handler {
        private WeakReference<DriverBaseActivity> mRef;
        public WeakHandler(DriverBaseActivity activity) {
            mRef = new WeakReference<DriverBaseActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            DriverBaseActivity activity = mRef.get();
            if (null == activity) {
                return;
            }

            ResultBean result = (ResultBean) msg.obj;
            if (null == result) {
                if (!activity.handleEmptyResult(msg.what)) {
                    Toast.makeText(activity, activity.getString(R.string.driver_tip_result_null),
                            Toast.LENGTH_SHORT).show();
                }
                return;
            }

            if (result.isSuccess()) {
                activity.handleSuccessResult(msg.what, result);
            } else {
                activity.handleFailureResult(msg.what, result);
            }

        }
    }

    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
    }

    protected void handleFailureResult(int what, @NonNull ResultBean result) {
        if (!TextUtils.isEmpty(result.getTitle())) {
            Toast.makeText(this, result.getTitle(), Toast.LENGTH_SHORT).show();
        }
    }

    protected boolean handleEmptyResult(int what) {
        return false;
    }
}
